<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>密码修改</title>
</head>
<link type="text/css" rel="stylesheet"
      href="../../bootstrap-4.4.1-dist/css/bootstrap.css">
<script src="../../js/jquery.js"></script>
<script src="../../bootstrap-4.4.1-dist/js/bootstrap.js"></script>
<style>
    body {
        user-select: none; /* 禁止文本被选中 */
        -webkit-user-select: none; /* Chrome浏览器的兼容性处理 */
        -moz-user-select: none; /* Firefox浏览器的兼容性处理 */
        -ms-user-select: none; /* IE/Edge浏览器的兼容性处理 */
    }
</style>
<body>
<nav class="navbar navbar-expand-lg navbar-light" style="background-color: white">
    <div class="container">
        <a class="navbar-brand" href="#">
            <img src="../../image/logo.png" alt="Logo" style="border-radius: 50%;" width="30" height="30"
                 class="d-inline-block align-top">
            SUN 图书馆
        </a>
    </div>
</nav>
<p style="height: 5px;background-color:cornflowerblue"></p>
<div style="width: 1000px;margin:20px auto;">
    <p><span style="background-color: #ba8b00">&nbsp;</span>&nbsp;密码修改</p>
    <div style="width: 500px;margin: 60px auto">
        <div class="mb-3">
            <label for="oldPassword" class="form-label">请输入原密码：</label>
            <input type="password" class="form-control" id="oldPassword">
        </div>
        <div class="mb-3">
            <label for="newPassword" class="form-label">请输入新密码：</label>
            <input type="password" class="form-control" id="newPassword">
        </div>
        <div class="mb-3">
            <label for="confirmNewPassword" class="form-label">请再次输入新密码：</label>
            <input type="password" class="form-control" id="confirmNewPassword">
        </div>
        <br>
        <p id="changePwd" style="text-align: center"><input style="width: 100px;height: 30px;border-radius: 5px;border: none;background-color: #333;color: white" type="button" value="修改"></p>
    </div>
</div>
</body>
</html>
<script>
    $('#changePwd').click(function (){
        let pwd = $('#oldPassword').val()
        let newPwd1 = $('#newPassword').val()
        let newPwd2 = $('#confirmNewPassword').val()
        if (verify(pwd,newPwd1,newPwd2)){
            let opr = "changePwd"
            $.ajax({
                'url': '../../ReaderServlet.action',
                'type': 'post',
                'data': {opr,pwd,newPwd2},
                'dataType': 'json',
                'success': changePwdSuccess
            })
        }
    })
    function changePwdSuccess(data) {
        if (data == 'ok'){
            alert("修改成功!")
            window.location.href = '../../html/login/login.html'
        }else if (data == 'oldno'){
            alert("原密码错误!")
        }
        else {
            alert("修改失败!")
        }
    }
    function verify(pwd,newPwd1,newPwd2){
        if (pwd ==''){
            alert("请输入原密码！")
            return false
        }
        if (newPwd1 ==''){
            alert("请输入新密码！")
            return false
        }
        if (newPwd2 ==''){
            alert("请再次输入新密码！")
            return false
        }
        if (pwd == newPwd1){
            alert("新密码不可以和原密码相同!")
            return false
        }
        if (newPwd1!==newPwd2){
            alert("请保证两次新密码相同!")
            return false
        }
        return true
    }
</script>